
import logging

import allure
import pytest
from selenium import webdriver

@pytest.fixture(scope="function")
def browser():
    driver=webdriver.Chrome()
    driver.maximize_window()
    yield driver
    driver.quit()

# 生成截图
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item,call):
    outcome=yield
    report=outcome.get_result()
    if report.when=="call":
        logging.info("-----日志的头部---------")
        logging.info(f"用例ID：{report.nodeid}")
        logging.info(f"用例结果：{report.outcome}")
        logging.info(f"故障标识：{report.longrepr}")
        logging.info(f"异常信息：{call.excinfo}")
        logging.info(f"用例耗时：{report.duration}")
        logging.info("-----日志的尾部---------")
        allure_text=(
            f"用例ID：{report.nodeid}\n"
            f"用例结果：{report.outcome}\n"
            f"故障标识：{report.longrepr}\n"
            f"异常信息：{call.excinfo}\n"
            f"用例耗时：{report.duration}\n"
        )

        allure.attach(allure_text, name="测试用例详细信息", attachment_type=allure.attachment_type.TEXT)
    if report.when=="call" and (report.failed or report.passed):
        driver=item.funcargs.get("browser")
        if driver:
            if report.failed:
                with allure.step("添加失败的截图"):
                    allure.attach(driver.get_screenshot_as_png(),"失败截图",allure.attachment_type.PNG)
            elif report.passed:
                with allure.step("添加成功的截图"):
                    allure.attach(driver.get_screenshot_as_png(), "成功截图", allure.attachment_type.PNG)

# pom框架不能复用，每个项目的页面操作都不一样，web自动化，代码能力比较好
# 关键字驱动的方式 web自动化 代码基础不好
# 用例写在excel /yaml  点击 输入步骤

# 还有1  没有2